package com.scrx.ymg.dao.distribution;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.scrx.ymg.dao.base.BaseDaoImpl;
import com.scrx.ymg.entity.DirectMoney;
import com.scrx.ymg.entity.Distribution;
import com.scrx.ymg.entity.OrderForm;


@Repository("distributionDao")
public class DistributionDaoImpl extends BaseDaoImpl  implements IDistributionDao {


	public Distribution getDistribution(int userID) {
		Query query=getCurrentSession().createQuery("from Distribution where userid=:userID");
		query.setParameter("userID",userID);
		return (Distribution)query.uniqueResult();
	}


	public String updateDistribution(Distribution distribution) {
		try {
			getCurrentSession().update(distribution);
			return "true";
		} catch (Exception e) {
            return "false";
		}
	}


	public String insertDistribution(Distribution distribution) {
		try {
			getCurrentSession().save(distribution);
			return "true";
		} catch (Exception e) {
            return "false";
		}
	}



	public String updateDistributionAndOrder(Distribution distribution,Distribution distribution2, OrderForm oldorder,DirectMoney directMoney) {
		try {
			if(distribution!=null){
				getCurrentSession().update(distribution);
			}
			if(distribution2!=null){
				getCurrentSession().update(distribution2);
			}
			getCurrentSession().update(directMoney);
			getCurrentSession().update(oldorder);
			return "true";
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "false";
		}
	}

}
